package com.longge.wechat.controller.web;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.longge.wechat.entity.UserMessageEntity;
import com.longge.wechat.entity.UsmReport;
import com.longge.wechat.service.UserMessageService;
import com.longge.wechat.utils.ExcelExport;

/***
 * @author ywl
 */
@Controller
public class ExportExcelController {
	@Resource
	private UserMessageService userMessageServiceImpl;

	// 导出商铺列表（POI方式）
	@RequestMapping(value ="/getShopReport.do",method = RequestMethod.GET)
	public void getOeuserInfoReport(HttpServletRequest request,HttpServletResponse response) {
		 String fileName = request.getParameter("name");
		
		 /*try { 
			 //fileName = new String(request.getParameter("name").getBytes("iso8859-1"),"utf-8");
			 fileName = new String(request.getParameter("name").getBytes("gb2312"),"gbk"); 
		 }
		 catch (UnsupportedEncodingException e) 
		 { 
			 e.printStackTrace(); 
		 }*/
		 
		int fromIndex = 1;
		List<UserMessageEntity> list = userMessageServiceImpl.findAllUserMessage(5,0);

		// 进行数据转换
		// List<UsmReport> listr = new ArrayList<UsmReport>();
		List<UserMessageEntity> listr = new ArrayList<UserMessageEntity>();
		for (UserMessageEntity ume : list) {
			// UsmReport ur =new UsmReport();
			UserMessageEntity ur = new UserMessageEntity();

			ur.setContent(ume.getContent());
			ur.setCreateTime(ume.getCreateTime());
			ur.setMsgType(ume.getMsgType());
			ur.setOpenId(ume.getOpenId());
			ur.setX(ume.getX());
			ur.setY(ume.getY());

			listr.add(ur);

		}

		try {
			new ExcelExport<UserMessageEntity>(UserMessageEntity.class).exportExcel(request, response, listr, fileName, fromIndex);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	// 导出商铺列表(JXL方式)
	@RequestMapping("/getShopReport1.do")
	public void getOeuserInfoReport1(HttpServletRequest request,HttpServletResponse response) {
		String fileName = "";
		try {
			// fileName = new
			// String(request.getParameter("name").getBytes("iso8859-1"),"utf-8");
			fileName = new String(request.getParameter("name").getBytes("gb2312"), "gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String[] Title = { "发信人Id", "消息类型", "消息创建时间", "消息内容", "X", "Y" };
		List<UserMessageEntity> list = userMessageServiceImpl.findAllUserMessage(5,0);

		// 进行数据转换
		// List<UsmReport> listr = new ArrayList<UsmReport>();
		List<UsmReport> listr = new ArrayList<UsmReport>();
		for (UserMessageEntity ume : list) {
			// UsmReport ur =new UsmReport();
			UsmReport ur = new UsmReport();

			ur.setContent(ume.getContent());
			ur.setCreateTime(ume.getCreateTime());
			ur.setMsgType(ume.getMsgType());
			ur.setOpenId(ume.getOpenId());
			ur.setX(ume.getX());
			ur.setY(ume.getY());

			listr.add(ur);

		}
		new ExcelExport<UsmReport>(UsmReport.class).JxlExportExcel(request, response,fileName, Title, listr);
	}

}
